
package com.android.formula.utils;

import android.content.Context;
import android.content.pm.ApplicationInfo;
import android.content.pm.PackageManager;
import android.os.Handler;
import android.util.Log;
import android.view.View;

public class AppUtils {
    private static final String TAG = "AppUtils";

    public static boolean isDebuggable(Context context) {
        if (context == null) {
            return false;
        }
        boolean debuggable = false;
        PackageManager pm = context.getPackageManager();
        try {
            ApplicationInfo appinfo = pm.getApplicationInfo(context.getPackageName(), 0);
            debuggable = (0 != (appinfo.flags &= ApplicationInfo.FLAG_DEBUGGABLE));
        } catch (PackageManager.NameNotFoundException e) {
            LogUtils.logError(TAG, Log.getStackTraceString(e));
        }
        return debuggable;
    }

    public static String getApplicationName(Context context) {
        if (context == null) {
            return "";
        }
        int stringId = context.getApplicationInfo().labelRes;
        return context.getString(stringId);
    }

    public static String getAppVersion(Context context) {
        if (context == null) {
            return "";
        }
        String appVersion = "";
        try {
            appVersion = context.getPackageManager().getPackageInfo(context.getPackageName(), 0).versionName;
        } catch (PackageManager.NameNotFoundException e) {
            LogUtils.logError(TAG, Log.getStackTraceString(e));
        }
        return appVersion;
    }

    /**
     * Used to check can click on the view or not. If the view is click able, we
     * will hide click able of it and reset again after a time.
     *
     * @param view the view that receive click action.
     * @return true: can click on this view. Otherwise, cannot click on this
     *         view.
     */
    public static boolean startClickView(final View view) {
        if (view == null || !view.isClickable()) {
            return false;
        }

        view.setClickable(false);

        // delay for next click.
        new Handler().postDelayed(new Runnable() {

            @Override
            public void run() {
                view.setClickable(true);
            }
        }, Constants.DURATION_CLICKABLE_FOR_VIEW);

        return true;
    }

}
